// pages/reserve/record/index.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        type:1,
        list:[],
        pagination:{page:1},
        topNavH:'',
        orderList:[],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      const that = this;
      wx.getSystemInfo({
        success: res => {
          var statusBarHeight = res.statusBarHeight;
          // console.log('statusBarHeight:' + statusBarHeight)
          var topNavH = (statusBarHeight + 45);
          that.setData({
            topNavH: topNavH + 'px',
          })
        }
      })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getRecords();
        this.getActivityRecords();
    },
    getRecords(){
        const that = this;
        wx.request({
          url: app.globalData.apiUrl + 'api/reserve/record',
          method: 'POST',
          data: {
            'openid':wx.getStorageSync('openid'),
            'status':that.data.type,
            'page':that.data.pagination.page
          },
          header: {
            'Content-Type': 'application/json'
          },
          success: function (res) {
            console.log(res.data)
            let response = res.data.data;
            that.setData({
                list:response
            })
          },
          fail: function () {
            
          },
          complete: function () {
          }
        })
      },
      
    getActivityRecords(){
      const that = this;
      wx.request({
        url: app.globalData.apiUrl + 'api/activity/orderList',
        method: 'POST',
        data: {
          'openid':wx.getStorageSync('openid'),
          'page':that.data.pagination.page
        },
        header: {
          'Content-Type': 'application/json'
        },
        success: function (res) {
          console.log(res.data)
          let response = res.data.data;
          that.setData({
            orderList:response
          })
        },
        fail: function () {
          
        },
        complete: function () {
        }
      })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    setActive(e){
        let type = e.currentTarget.dataset.type;
        if(this.data.type != type) {
            this.data.list = [];
        }
        this.setData({
            type:e.currentTarget.dataset.type
        })
        if(this.data.type == 1) {
          this.getRecords();
        }
        else {
          this.getActivityRecords();
        }
        
    },
    cancelReserve(e){
        let id = e.currentTarget.dataset.id;
        const that = this;
        wx.showModal({
          title: '预约取消',
          content: '确定取消预约吗？',
          complete: (res) => {
            if (res.cancel) {
              
            }
        
            if (res.confirm) {
                that.cancaleReserveApi(id);
            }
          }
        })
    },
    cancaleReserveApi(id){
        const that = this;
        wx.request({
          url: app.globalData.apiUrl + 'api/reserve/cancel',
          method: 'POST',
          data: {
            'openid':wx.getStorageSync('openid'),
            'logid':id,
          },
          header: {
            'Content-Type': 'application/json'
          },
          success: function (res) {
            console.log(res.data)
            let response = res.data;
            wx.showToast({
              icon:'none',
              title: response.msg,
            })
            that.getRecords();
          },
          fail: function () {
            
          },
          complete: function () {
          }
        })
    },
    reserveInfo(e) {
        let type = e.currentTarget.dataset.type
        let id = e.currentTarget.dataset.id
        if(type ==1 ) {//散客
            wx.navigateTo({
              url: '/pages/reserve/personal/index?type='+type+'&logid='+id,
            })
        }
        else if(type == 2 ) {//团客
          wx.navigateTo({
            url: '/pages/reserve/team/index?type='+type+'&logid='+id,
          })
        }
    },
    navigateDetail(e){
      let oid = e.currentTarget.dataset.oid;
      wx.navigateTo({
        url: '/pages/bm/order/index?oid='+oid,
      })
    }
})